﻿import game.DataManager;
import game.GUI;
/**
* ...
* @author Default
*/

class game.Timer {
	
	private var _nInterval:Number;
	private var _mcTimer:MovieClip;
	private var _fCallBack:Function;
	private var _nSeconds:Number;
	
	public function Timer(mc:MovieClip, time:Number, callBack:Function) {
		_mcTimer = mc;
		_mcTimer._smcSparks._visible = false;
		_fCallBack = callBack;
		_mcTimer._parent._sbtTime._sbtTime.text = "0:00";
	}
	
	public function nextFrame() {
		
		if (_mcTimer._currentframe < 499) {
			_mcTimer.nextFrame();
			_mcTimer._smcSparks.nextFrame();
			_mcTimer._parent._smcRoundScore._stxRoundScore.text = String(2000 - _mcTimer._currentframe * 4);
		} else {
			_mcTimer._smcSparks._visible = false;
			clearInterval(_nInterval);
			_fCallBack();
			_mcTimer._parent._smcRoundScore._stxRoundScore.text = "0";
		}
		
	}
	
	public function startMainTimer(seconds:Number):Void {
		_nSeconds = seconds;
		secondPassed();
		setInterval(this, "secondPassed", 1000)
	}
	
	private function secondPassed():Void {
		_nSeconds--;
		var displayedTime:String = Math.floor(_nSeconds / 60) + ":" + (((_nSeconds % 60) < 10)? "0" : "") + (_nSeconds % 60);
		_mcTimer._parent._sbtTime._sbtTime.text = displayedTime;
	}
	
	public function start(time:Number):Void
	{
		_mcTimer._smcSparks._visible = true;
		_nInterval = setInterval(this, "nextFrame", time / 500 * 1000);
	}
	
	public function reset():Void {
		_mcTimer.gotoAndStop (1);
		_mcTimer._smcSparks.gotoAndStop (1);
		_mcTimer._parent._smcRoundScore._stxRoundScore.text = "2000";
		clearInterval(_nInterval);
	}
	
	public function getRoundScore():Number {
		return 2000 - _mcTimer._currentframe * 4;
	}
	
	public function stop():Boolean {
		clearInterval(_nInterval);
		DataManager.getInstance().addToScore(getRoundScore());
		GUI.getInstance().updateScore();
		if (_mcTimer._currentframe <= 165) {
			_mcTimer._parent._smcBonus.gotoAndPlay (22);
			_nSeconds += 10;
			return true;
		}
		
		return false;
	}
	
}